/** marketId 商场ID
 * *urlSort、urlcatalog 请求地址
 * *requestSort, requestCatalog 请求主体
 * */
mui.init();
var marketId = readData(localStorage.marketId);
var urlSort = getUrl("74getCatalogs");
var urlcatalog = getUrl("94catalogFind");
var requestSort, requestCatalog;
var demo = new Vue({
	el: '#main',
	data: {
		showmy: false,//是否显示我的关注
		myattions: [],//我关注的品牌
		recommends: [],//品牌列表
		sortRecmment: {},//分类推荐
		sorts: []//分类
	},
	methods: {
		init: init,
		loginJudge: loginJudge,
		detail: detail
	},
	ready: function() {/*分类请求*/
		loginJudge();
		this.$http(requestSort).then(function(response) {
			console.log(JSON.stringify(response.data));
			var lists = response.data.obj.systemcataloglist;
			demo.sortRecmment = lists[0];
			init(lists[0]);
			for (var i = 1; i < lists.length; i++) {
				demo.sorts.push(lists[i]);
			}
		}, function(response) {
			console.log('圈子分类错误' + JSON.stringify(response));
		});
	}
});
/*读取品牌信息*/
function init(mainId) {
	loginJudge(mainId);
	plus.nativeUI.showWaiting('加载中...');
	demo.$http(requestCatalog).then(function(response) {
		setTimeout(function(){
		  plus.nativeUI.closeWaiting();	
		},200)		
		console.log(JSON.stringify(response.data));
		demo.myattions = [];
		demo.recommends = [];
		var mycrcls = response.data.obj.myAttentions;
		var recomcrcls = response.data.obj.noAttentions;
		if (localStorage.account) {
			if (mycrcls.length > 0) {
				demo.showmy = true;
				for (var i = 0; i < mycrcls.length; i++) {
					demo.myattions.push(mycrcls[i]);
				}
			}
		}
		for (var i = 0; i < recomcrcls.length; i++) {
			demo.recommends.push(recomcrcls[i]);
		}
	}, function(response) {
		plus.nativeUI.closeWaiting();
		console.log('读取圈子出错' + JSON.stringify(response.data.obj));
	});
}
//打开圈子页or圈子详情页
function detail(alls, type) {
	if (getlogin('../my/loginHead.html')) {
		if (type == 1) { //打开圈子
			openCircle({
				urls: 'circleMain.html',
				circleId: alls.circleId,
				guideId: alls.userId,
				circleName: alls.circleName,
				groupId: alls.groupId
			});
		} else { //打开圈子详情页
			openGuanzhu({
				uid: alls.userId,
				urls: 'guanzhu.html'
			});
		}
	}
}
//登录判断
function loginJudge(mainId) {
	if (localStorage.account) { //已登录
		urlSort = getUrl("74getCatalogs");
		var headsort = getHeader();
		var aid = readData(localStorage.account);
		requestSort = {
			url: urlSort,
			method: 'POST',
			headers: headsort,
			params: {
				"marketId": marketId,
				"accountId": aid
			},
			timeout: 10000
		};
		urlcatalog = getUrl("94catalogFind");
		var headCatalog = getHeader();
		if (mainId) {
			requestCatalog = {
				url: urlcatalog,
				method: 'POST',
				headers: headCatalog,
				params: {
					"marketId": marketId,
					"accountId": aid,
					"catalogId": mainId.id
				},
				timeout: 10000
			};
		}
	} else { //未登录
		requestSort = {
			url: urlSort,
			method: 'POST',
			params: {
				"marketId": marketId
			},
			timeout: 10000
		};
		if (mainId) {
			requestCatalog = {
				url: urlcatalog,
				method: 'POST',
				params: {
					"marketId": marketId,
					"catalogId": mainId.id
				},
				timeout: 10000
			};
		}
	}
}
window.addEventListener("reloads",function(){
	location.reload();
})